/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iss.examsys.view;

import com.iss.examsys.entity.User;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.push.EventBus;
import org.primefaces.push.EventBusFactory;

/**
 *
 * @author User
 */
@ManagedBean
@Named
public class UserExamSessionData {
    
    @Inject InvigilateExamView invigilateExamReview;
    
    /** Check for logged on Users **/
    private User loggedOnUser;
    
    /** Check for the Number of question Answered**/
    private int questionAnswered=0;
    
    /**Check for exam submission **/
    private boolean hasSubmitted;

    public User getLoggedOnUser() {
        return loggedOnUser;
    }

    public void setLoggedOnUser(User loggedOnUser) {
        this.loggedOnUser = loggedOnUser;
    }

    public int getQuestionAnswered() {
        return questionAnswered;
    }

    public void setQuestionAnswered(int questionAnswered) {
        this.questionAnswered = questionAnswered;
    }

    public boolean isHasSubmitted() {
        return hasSubmitted;
    }

    public void setHasSubmitted(boolean hasSubmitted) {
        this.hasSubmitted = hasSubmitted;
    }
   
    /**
     * Update the number of question the user has answered
     */
    public void updateQuestionAnswered(){
        invigilateExamReview.updateStudentExamStatus(loggedOnUser.getUserId()
                , questionAnswered);
        EventBus bus = EventBusFactory.getDefault().eventBus();
        bus.publish("/invigilateLoggedOn",loggedOnUser);
        bus.publish("/invigilateQuestionAnswered", questionAnswered);     
    }
    
     /**
     * Update the user that have logged in to the
     * exam session.
     */
    public void updateLoggedOnUsers(){
        invigilateExamReview.updateLoggedOnUsers(loggedOnUser);
        EventBus bus = EventBusFactory.getDefault().eventBus();
        bus.publish("/invigilateLoggedOn",loggedOnUser);
    }
    
    /**
     * Update the student submission status
     */
    public void updateCompleteExam(){
        invigilateExamReview.updateStudentSubmissionStatus(null, hasSubmitted);
        EventBus bus = EventBusFactory.getDefault().eventBus(); 
        bus.publish("/invigilateLoggedOn",loggedOnUser);
        bus.publish("/invigilateSubmitted", hasSubmitted);  
    }
    
    
}
